package com.talkweb.osmharmony;

import ohos.sensor.agent.CategoryOrientationAgent;
import ohos.sensor.bean.CategoryOrientation;
import ohos.sensor.data.CategoryOrientationData;
import ohos.sensor.listener.ICategoryOrientationDataCallback;

public class SensorEventListenerProxy implements ICategoryOrientationDataCallback {

    private final CategoryOrientationAgent mCategoryOrientationAgent;
    private ICategoryOrientationDataCallback mCallback;

    public SensorEventListenerProxy(final CategoryOrientationAgent pCategoryOrientationAgent) {
        mCategoryOrientationAgent = pCategoryOrientationAgent;
    }

    public boolean startListening(final ICategoryOrientationDataCallback pCallback, final int pSensorType,
                                  final int pInterval) {

        final CategoryOrientation orientationSensor = mCategoryOrientationAgent.getSingleSensor(pSensorType);
        if (orientationSensor == null)
            return false;

        mCallback = pCallback;
        return mCategoryOrientationAgent.setSensorDataCallback(this, orientationSensor, pInterval);
    }

    public void stopListening() {
        mCallback = null;
        mCategoryOrientationAgent.releaseSensorDataCallback(this);
    }

    @Override
    public void onSensorDataModified(CategoryOrientationData categoryOrientationData) {
        if (mCallback != null) {
            mCallback.onSensorDataModified(categoryOrientationData);
        }
    }

    @Override
    public void onAccuracyDataModified(CategoryOrientation categoryOrientation, int index) {
        if (mCallback != null) {
            mCallback.onAccuracyDataModified(categoryOrientation, index);
        }
    }

    @Override
    public void onCommandCompleted(CategoryOrientation categoryOrientation) {
        if (mCallback != null) {
            mCallback.onCommandCompleted(categoryOrientation);
        }
    }
}
